<?php


if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class m_pengguna extends CI_Model {

	function ambil() {
		$ambil = $this->db->get_where('user', array('status' => '1' ));
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}

	function ambilbyid($id="") {
		$ambil = $this->db->get_where('user', array('id_user' => $id));
		return $ambil;
	}

	function update($id="")
	{
		$nama = $this->input->post('nama');
		$username = $this->input->post('username');
		$email = $this->input->post('email');
		$no_hp = $this->input->post('no_hp');
		$password = $this->input->post('password');
		$data1 = array('nama' => $nama,'username' => $username, 'email' => $email, 'nomor_hp' => $no_hp, 'password' => $password);
		//transact sql e bro
		$this->db->trans_start();
		$this->db->where('id_user', $id);
		$this->db->update('user', $data1);
		$this->db->trans_complete();
		$this->session->set_flashdata('result_pass_true', 'Data Pengguna berhasil diperbarui');
		header('location:' . site_url('pengguna/edit'.$id));
	}	


	function tambah(){
		$nama = $this->input->post('nama');
		$username = $this->input->post('username');
		$email = $this->input->post('email');
		$no_hp = $this->input->post('no_hp');
		$password = $this->input->post('password');
		$status = '1';
		$data1 = array('nama' => $nama,'username' => $username, 'email' => $email, 'nomor_hp' => $no_hp, 'password' => $password, 'status' => $status);
		$this->db->trans_start();
		$this->db->insert('user', $data1);
		$this->db->trans_complete();
	}


	function hapus($a) {
		$this->db->trans_start();
		$this->db->where('id_user', $a);
		$this->db->update('user', array('status' => '0' ));
		$this->db->trans_complete();
		return;
	}


	function login($username, $pass) {
		$cek_login = $this->db->get_where('user', array('username' => $username, 'password' => $pass, 'status' => '1'));
		if (count($cek_login->result()) > 0) {
			foreach ($cek_login->result_array() as $data) {
				$hasil[] = $data;
			}
			return true;
		} else {
			return false;
		}
	}

	function get_id_pemilik($nama){
		$this->db->select('id_pemilik_hewan');
		$this->db->from('pemilik_hewan');
		$this->db->where('nama', $nama);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}
}
